import pygame
import copy

class Messages:
	def __init__(self, screen):
		self.screen = screen
		self.surface = pygame.Surface((screen.get_width(), 50))
		self.surface.fill((0,0,0))
		self.font = pygame.font.Font(None, 20)
		self.message = ""

	def paint(self):
		messages = self.message.split('\n')
		surface = copy.copy(self.surface)
		for i,m in enumerate(messages):
			text = self.font.render(m, 1, (255,255,255))
			surface.blit(text, (10, (i*20)))
		self.screen.blit(surface, (0, self.screen.get_height()-self.surface.get_height()))

	def put_msg(self, msg):
		self.message = msg

	def get_msg(self, head=""):
		string = ""
		while 1:
			surface = copy.copy(self.surface)
			text = self.font.render(head+string, 1, (255,255,255))
			surface.blit(text, (10, 10))
			self.screen.blit(surface, (0, self.screen.get_height()-self.surface.get_height()))

			pygame.display.flip()

			keys = pygame.event.wait()
			if keys.type == pygame.KEYDOWN and keys.key == pygame.K_ESCAPE:
				return 0
			elif keys.type == pygame.KEYDOWN and keys.key == pygame.K_BACKSPACE:
				string = string[0:-1]
			elif keys.type == pygame.KEYDOWN and keys.key == pygame.K_RETURN:
				return string
			elif keys.type == pygame.KEYDOWN and keys.key == pygame.K_SPACE:
				string = string + ' '
			elif keys.type == pygame.KEYDOWN:
				key = keys.key
				string = string + keys.unicode
					
		return string



		
